<template>
  <el-pagination
    class="ui-pagination"
    layout="total, sizes, ->, prev, pager, next, jumper"
    :page-sizes="pageSizeList"
    :page-size="innerPageSize"
    :current-page="pageIndex"
    :total="total"
    @current-change="handlePageIndexChange"
    @size-change="handlePageSizeChange"
  ></el-pagination>
</template>
<script>
import Pagination from 'element-ui/lib/pagination'
import 'element-ui/lib/theme-chalk/pagination.css'

export default {
  name: 'UiPagination',
  components: {
    ElPagination: Pagination
  },
  props: {
    pageSize: {
      type: Number,
      default: 30
    },
    pageIndex: {
      type: Number,
      default: 0
    },
    total: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      innerPageSize: this.pageSize
    }
  },
  computed: {
    pageSizeList () {
      return [30, 50, 100]
    }
  },
  methods: {
    // 每页显示行数发生变化触发
    handlePageSizeChange (value) {
      if (this.innerPageSize === value) {
        return
      }
      this.innerPageSize = value
      const pageIndex = Math.max(Math.min(
        Math.ceil(this.total / value),
        this.pageIndex
      ), 1)
      this.$emit('page-change', {
        pageIndex,
        pageSize: value
      })
    },
    // 分页改变时触发
    handlePageIndexChange (value) {
      const pageIndex = value
      if (this.pageIndex === pageIndex) {
        return
      }
      this.$emit('page-change', {
        pageIndex,
        pageSize: this.innerPageSize
      })
    }
  }
}
</script>
<style lang="less">
.ui-pagination.el-pagination {
  .btn-prev,
  .btn-next,
  .number,
  .more {
    min-width: 14px;
    height: 30px;
    line-height: 30px;
    border: 1px solid #d9d9d9;
    box-sizing: content-box;
    padding: 0 9px;
    border-radius: 3px;
    margin-left: 6px;
    font-weight: normal;
    &:hover {
      color: #00A0EB;
      border-color: #00A0EB;
    }
    &.active {
      border-color: #00A0EB;
      color: #00A0EB;
    }
    &[disabled] {
      border-color: #d9d9d9;
      color: #999;
    }
  }
  .more {
    color: #aaa;
    font-size: 16px;
    margin-left: 6px;
    &:hover {
      color: #00A0EB;
    }
  }
  .el-pagination__total{
    height: 32px;
    line-height: 32px;
  }
  .el-pagination__sizes,
  .el-pagination__jump {
    .el-input__inner {
      height: 32px;
      font-size: 14px;
    }
  }
}
</style>
